<?php
header("Content-type: text/javascript");

define( "D_FOLDER", "smiles/" );
define( "D_SITE", "http://ok.onlife.mobi/".D_FOLDER );

//$tbs = (int)( $_GET['tb'] );

$ret = array(
	"referrer"=>$_SERVER['HTTP_REFERER'],
	"error"=>0,
	"packs"=>array()
);


if ( $handle = opendir( "smiles" ) )
{
	while( ( $file = readdir( $handle ) ) !== false )
	{
		if ( $file == ".." || $file == "." ) continue;
		
		if ( file_exists( "smiles/" . $file . "/list.json" ) )
		{
			// Load info from the file
			ob_start();
			include "smiles/" . $file . "/list.json";
			$a = ob_get_contents();
			ob_clean();
			$a = json_decode($a);
			$ret['packs'][$file] = $a;
		}
		else
		{
			// Load all smiles from folder
			if ( $h = opendir( "smiles/" . $file ) )
			{
				$count = 0;
				while ( ( $f = readdir( $h ) ) !== false )
				{
					if ( $f == ".." || $f == "." ) continue;
					
					list( $n, $e ) = explode( ".", $f, 2 );
					
					if ( $e == "png" || $e == "gif" )
					{
						$ret['packs'][$file][$count] = array(
							"url"=>D_SITE.$file."/".$f,
							"symbols"=>"[".$n."]"
						);
					}
					$count++;
				}
			}
		}
	}

	closedir( $handle );
}


echo json_encode($ret);
